package com.meihua.shellapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.login.LoginActivity;
import com.meihua.shellapp.activity.webview.WebActivity;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.common.Constant;
import com.meihua.shellapp.common.NetHelper;
import com.meihua.shellapp.dialog.LoginandRegistrationDialog;
import com.meihua.shellapp.utils.SharedPreferencesUtil;
import com.meihua.shellapp.utils.URL;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 留言反馈
 */
public class OnLineQueryActivity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView ivTopLeft;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.online_et_message)
    EditText mEtMessage;
    private LoginandRegistrationDialog dialog;


    @Override
    public int getLayoutId() {
        return R.layout.activity_on_line_query;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText("留言反馈");
        ivTopLeft.setVisibility(View.VISIBLE);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.iv_top_left, R.id.online_btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_top_left:
                finish();
                break;
            case R.id.online_btn_submit:
                if (!SharedPreferencesUtil.getInstance().getBoolean(OnLineQueryActivity.this, Constant.ISLOGIN, false)) {
                    dialog = new LoginandRegistrationDialog.Builder(OnLineQueryActivity.this)
                            .setClickSure(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent = new Intent(OnLineQueryActivity.this, LoginActivity.class);
                                    startActivity(intent);
                                    dialog.dismiss();
                                }
                            })
                            .setClickCencle(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    startActivity(new Intent(OnLineQueryActivity.this, WebActivity.class).putExtra("url", URL.getUrl().REGISTER_URL));
                                    dialog.dismiss();
                                }
                            })
                            .setMessage("请您登录注册！")
                            .show();
                    return;
                }
                /*上传留言*/
                final String msg = mEtMessage.getText().toString().trim();
                if (TextUtils.isEmpty(msg)) {
                    Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
                } else {
                    Map<String, String> params = new HashMap() {
                        {
                            put("member_num", MyApplication.getInstance().getUserId());
                            put("comments_info", msg);
                        }
                    };
                    NetHelper.post(URL.getUrl().LYFK, params, new NetHelper.HttpCallBack() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            JSONObject obj = null;
                            try {
                                obj = new JSONObject(response.body());
                                if (obj.getInt("code") == 1)
                                    Toast.makeText(OnLineQueryActivity.this, "留言成功", Toast.LENGTH_SHORT).show();
                                finish();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    }, false);
                }
                break;
        }
    }
}
